package com.ruoyi.web.controller.check;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.check.domain.ECheckListFormXunjian;
import com.ruoyi.check.service.IECheckListFormXunjianService;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.check.domain.Sequence;
import com.ruoyi.check.service.ISequenceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 流水表Controller
 * @author hugh.sun
 * @date 2023-04-11
 */


@RestController
@RequestMapping("/check/sequence")
public class SequenceController extends BaseController
{
    @Autowired
    private ISequenceService sequenceService;

    @Autowired
    private IECheckListFormXunjianService eCheckListFormXunjianService;

    /**
     * 查询流水表列表
     */
    @PreAuthorize("@ss.hasPermi('check:sequence:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sequence sequence)
    {
        startPage();
        List<Sequence> list = sequenceService.selectSequenceList(sequence);
        return getDataTable(list);
    }

    @GetMapping("/list1")
    public String list1()
    {
        //已巡查
        Long yixunjian = sequenceService.yixunjian();
        //待巡查
        //所有巡检数减去已经巡检=待巡检
        ECheckListFormXunjian eCheckListFormXunjian = new ECheckListFormXunjian();
        List<ECheckListFormXunjian> es = eCheckListFormXunjianService.selectECheckListFormXunjianList(eCheckListFormXunjian);
        List<Integer> integers = new ArrayList<>();
        es.forEach(i->{
            Long yingxunjian = i.getYingxunjian();
            integers.add(Integer.valueOf(Math.toIntExact(yingxunjian)));
        });
        int sum = 0;
        for (Integer num : integers) {
            sum += num;
        }
        //待巡检
        long l = sum - yixunjian;

        return "已巡检"+yixunjian+"未巡检"+l;
    }

    /**
     * 导出流水表列表
     */
    @PreAuthorize("@ss.hasPermi('check:sequence:export')")
    @Log(title = "流水表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sequence sequence)
    {
        List<Sequence> list = sequenceService.selectSequenceList(sequence);
        ExcelUtil<Sequence> util = new ExcelUtil<Sequence>(Sequence.class);
        util.exportExcel(response, list, "流水表数据");
    }

    /**
     * 获取流水表详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:sequence:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sequenceService.selectSequenceById(id));
    }

    /**
     * 新增流水表
     */
    @PreAuthorize("@ss.hasPermi('check:sequence:add')")
    @Log(title = "流水表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sequence sequence)
    {
        return toAjax(sequenceService.insertSequence(sequence));
    }

    /**
     * 修改流水表
     */
    @PreAuthorize("@ss.hasPermi('check:sequence:edit')")
    @Log(title = "流水表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sequence sequence)
    {
        return toAjax(sequenceService.updateSequence(sequence));
    }

    /**
     * 删除流水表
     */
    @PreAuthorize("@ss.hasPermi('check:sequence:remove')")
    @Log(title = "流水表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sequenceService.deleteSequenceByIds(ids));
    }






}
